package com.atguigu.flink.chapter05.source;

import org.apache.flink.api.common.eventtime.WatermarkStrategy;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.connector.file.src.FileSource;
import org.apache.flink.connector.file.src.reader.TextLineInputFormat;
import org.apache.flink.core.fs.Path;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;


/**
 * @Author lzc
 * @Date 2023/6/17 14:45
 */
public class FileSourceDemo {
    public static void main(String[] args) {
        Configuration conf = new Configuration();
        conf.setInteger("rest.port", 2000);
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment(conf);
        env.setParallelism(1);
        
        // 创建一个 file source: 先获取 xxxBuilder ,再 build()可以得到对应的 source
        FileSource<String> source = FileSource
            .forRecordStreamFormat(new TextLineInputFormat(), new Path("input/words.txt"))
            .build();
        
        DataStreamSource<String> stream = env.fromSource(source,
                                                         WatermarkStrategy.noWatermarks(),
                                                         "file-source"
        );
        
        stream.print();
        
        try {
            env.execute();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
